package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.common.dialog.ECProgressDialog;
import com.yuntongxun.ecdemo.voicechat.api.ApiConstants;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.presenter.ICommonRequestPresenter;
import com.yuntongxun.ecdemo.voicechat.presenter.impl.CommonRequestPresenterImpl;
import com.yuntongxun.ecdemo.voicechat.util.InfoUtil;
import com.yuntongxun.ecdemo.voicechat.util.iview.ICommonViewUi;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 钱包
 *
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/18
 */

public class WalletActivity extends BaseChatActivity implements ICommonViewUi {


    @InjectView(R.id.walletCoins)
    TextView walletCoins;

    UserInfo userInfo;

    private ICommonRequestPresenter iCommonRequestPresenter;

    @OnClick(R.id.top_up_layout)
    public void onTopUpClick(){
        Intent intent=new Intent(this, TopUpActivity.class);
        startActivity(intent);
    }
    @OnClick(R.id.withdraw_layout)
    public void onWithdrawClick(){
        Intent intent=new Intent(this, WithdrawTransferActivity.class);
        intent.putExtra(WithdrawTransferActivity.PAY_COINS, walletCoins.getText().toString());
        startActivity(intent);
    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    public void onEvent(PostResult postResult){
        if (EventBusTags.PAY_SUCCESS.equals(postResult.getTag())){
            toRequest(ApiConstants.EventTags.BEGIN_EVENT);
        }
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_wallet;
    }

    @Override
    protected void initViewsAndEvents() {
        setTitle("Wallet");

        setRightTitle("Transactions");
        iCommonRequestPresenter = new CommonRequestPresenterImpl(mContext, this);
        userInfo= InfoUtil.getLocalUserInfo(this);
        toRequest(ApiConstants.EventTags.BEGIN_EVENT);

        mRightBtnTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext, TransactionsActivity.class);
                startActivity(intent);
            }
        });
    }


    @Override
    public void toRequest(int eventTag) {
        RequestParams map = new RequestParams();
        map.put("accountName", userInfo.getAccountName());
        iCommonRequestPresenter.request(eventTag, mContext, ApiConstants.Urls.ACCOUNT, map);
    }

    @Override
    public void getRequestData(int eventTag, String result) {
        try {
            JSONObject object=new JSONObject(result);
            String account =object.getString("result");
            walletCoins.setText(account);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestSuccessException(int eventTag, String msg) {
        showToastLong(msg);
        finish();
    }

    @Override
    public void onRequestFailureException(int eventTag, String msg) {
        showToastLong(msg);
        finish();
    }

    private ECProgressDialog mPostingdialog;
    @Override
    public void isRequesting(int eventTag, boolean status) {
        if(status){
            mPostingdialog = new ECProgressDialog(this, "Waiting");
            mPostingdialog.show();
        }else{
            mPostingdialog.dismiss();
        }
    }
}
